package org.arkeologgr.appening.activities;

import java.util.ArrayList;
import java.util.List;

import org.arkeologgr.appening.dataacess.DataAccessManager;
import org.arkeologgr.appening.dataacess.DataAccessManager.AddCultureItemListener;
import org.arkeologgr.appening.domain.CultureItem;
import org.arkeologgr.appening.domain.CultureItemType;

import org.arkeologgr.appening.R;
import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddItemActivity extends Activity{
	
	public static final int ACTIVITY_RESPONSE_OK = 1;
	public static final int ACTIVITY_RESPONSE_FAILED = 2;
	private CultureItem generatedItem;
	
	private EditText nameField;
	private EditText tagField;
	private EditText descriptionField;
	private Bitmap bitmap;
	private List<String> tags;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add);
		bitmap = (Bitmap)getIntent().getExtras().get("image");
		int lat = getIntent().getIntExtra("lat", 0);
		int lon = getIntent().getIntExtra("lon", 0);
		GeoPoint point = new GeoPoint(lat, lon);
		
		generatedItem = new CultureItem();
		generatedItem.setGeoPoint(point);
		
		
		nameField = (EditText)findViewById(R.id.nameField);
		tagField = (EditText)findViewById(R.id.tagField);
		ImageView image = (ImageView)findViewById(R.id.AddImage);
		image.setImageBitmap(bitmap);
		descriptionField = (EditText)findViewById(R.id.descriptionField);
		tags = new ArrayList<String>();
		
		final LinearLayout tagLayout = (LinearLayout)findViewById(R.id.tagLayout);
		
		findViewById(R.id.addTagButton).setOnClickListener(new OnClickListener (){

			public void onClick(View v) {
				tags.add(tagField.getText().toString());
				TextView text = new TextView(AddItemActivity.this);
				text.setText(tagField.getText().toString());
				text.setTextColor(Color.BLACK);
				tagLayout.addView(text);
			}
			
		});
		
		findViewById(R.id.send).setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				generatedItem.setDescription(descriptionField.getEditableText().toString());
				generatedItem.setTitle(nameField.getEditableText().toString());
				generatedItem.setTags(tags);
				generatedItem.setType(CultureItemType.UserGeneratedItem);
				DataAccessManager.getInstance().addCultureItem(generatedItem,bitmap, new AddCultureItemListener() {
					
					public void onCultureItemAdded() {
						finishOk();
					}
					
					public void onCultureItemAddFail(String reason) {
						finishWithError(reason);
					}
				});
				
			}
		});	
	}
	
	private void finishOk(){
		setResult(RESULT_OK);
		finish();
	}
	
	private void finishWithError(String error){
		setResult(RESULT_CANCELED);
		Intent intent = getIntent();
		intent.putExtra("error", error);
	}
}
